package com.ruoyi.project.cms.type.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.cms.type.mapper.CmsTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CmsTypeServiceImpl implements ICmsTypeService {

    @Autowired
    private CmsTypeMapper cmsTypeMapper;

    @Override
    public List<CmsType> selectPostList(CmsType cmsType) {
        return cmsTypeMapper.selectPostList(cmsType);
    }

    @Override
    public String checkTypeNameUnique(CmsType cmsType) {
        Long postId = StringUtils.isNull(cmsType.getTypeId()) ? -1L : cmsType.getTypeId();
        System.out.println(postId);
        CmsType info = cmsTypeMapper.checkTypeNameUnique(cmsType.getTypeName());
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != postId.longValue())
        {
            return UserConstants.CMS_TYPE_NOT_UNIQUE;
        }
        return UserConstants.CMS_TYPE_UNIQUE;

    }

    @Override
    public int insertCmsType(CmsType cmsType) {
       return  cmsTypeMapper.insertCmsType(cmsType);
    }

    @Override
    public CmsType selectCmsTypeById(long typeId) {
        return cmsTypeMapper.selectCmsTypeById(typeId);
    }

    @Override
    public int updateCmsType(CmsType cmsType) {
        return cmsTypeMapper.updateCmsType(cmsType);
    }

    @Override
    public int deletePostByIds(String ids) {
//        Long[] split = ids.split(",");
        Long[] typeIds = Convert.toLongArray(ids);
        if(typeIds.length > 0){
            return cmsTypeMapper.deletePostByIds(typeIds);
        }
        return 0;
    }
}
